<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编辑文章</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <!-- jq -->
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/layui.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            编辑文章
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" value="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img id="img" src="./images/2.jpg" class="article_cover">
                        <input name="cover" type="file" id="inputCover" onchange="changeImg()">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select id="category" class="form-control category" name="categoryId">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <input id="articleDate" name='date' class="jeinput" type="text" placeholder="请选择发布日期" readonly>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <!-- 富文本编辑区域 -->
                        <textarea id="articleContent">请输入文章内容</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" id="edit" class="btn btn-success btn-edit">修改</button>
                        <button type="submit" id="edit-c" class="btn btn-default btn-draft">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>
    <script src="./js/http.js"></script>
    <script src="./libs/art-template/template-web.js"></script>
    <!-- 定义文章分类数据模板 -->
    <script id="art_cate" type="text/html">
        {{each data}}
        <option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>

    <script>
        $(function () {
            // 2.日期
            init_date();
            // 3.副文本编辑器
            init_textarea();
            // 4.文章类别编辑器
            get_cate_list();

            // 给修改和存为草稿添加点击事件
            $('#edit').click(function (e) {
                e.preventDefault();
                save('已发布');
            })
            $('#edit_c').click(function (e) {
                e.preventDefault();
                save('')
            })
        })
        function changeImg() {
            let file = document.querySelector('#inputCover').files[0];
            let path = URL.createObjectURL(file);
            $('#img').attr('src', path);
        }

        // 2. 初始化日期控件
        function init_date() {
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#articleDate' //指定元素
                });
            });
        }

        // 3. 初始化富文本编辑器
        function init_textarea() {
            tinymce.init({
                selector: '#articleContent',
                language: 'zh_CN'
            });
        }

        // 4. 初始化文章类别的下拉列表
        function get_cate_list() {
            myAjax('get', '/admin/category/list', null, function (resData) {
                let resHtml = template('art_cate', resData);
                $('#category').html(resHtml);
                // 当文章下拉列表数据响应成功以后，再获取文章老数据
                get_info_by_id();
            })
        }

        // 5. 根据文章id获取文章老数据请求
        function get_info_by_id() {
            // 5.1 解析出url地址中的id 
            // let p_id = get_params()['id'];   //---6
            let p_id = get_params();   //---6
            // console.log("-===--p_id--===-");
            // console.log(p_id);

            // 5.2根据id请求接口：/admin/article/search 获取到文章老数据
            myAjax('get', '/admin/article/search', { id: p_id }, function (resData) {
                // 将文章老数据一一赋值给页面的对应元素
                // console.log("----resData-----" + resData);
                // console.log(resData)
                let data = resData.data;
                // console.log("----data-----");
                // console.log(data)
                $('#inputTitle').val(data.title);
                $('#img').attr('src', data.cover);
                $('#category').val(data.categoryId);
                $('#articleDate').val(data.data);
                // 注意：富文本的赋值方式写法如下：
                setTimeout(() => {
                    tinyMCE.editors['articleContent'].setContent(data.content);
                }, 300)
            })
        }

        // 6.获取到window.location.href中的id参数值
        function get_params() {
            // file://dddd/ddd/article_edit.html?id=225
            let url = window.location.href;
            let urlArr = url.split('?');
            // console.log("-----url----");
            // console.log(url);
            if (urlArr.length <= 1) {
                alert('请输入正确的id参数');
                return;
            }
            // 获取到参数部分
            let parms = urlArr[1];
            // 
            let parmArr = parms.split('=');
            let id_value = parmArr[1];
            // console.log("----id_value--");
            // console.log(id_value);
            return id_value;
        }

        function save(type) {
            // 7.1 收集文本框的数据
            let title = $('#inputTitle').val();
            let cover = document.querySelector('#inputCover').files[0];
            let categoryId = $('#category').val();
            let date = $('#articleDate').val();
            let content = tinyMCE.editors['articleContent'].getContent();
            let state = type;

            // 7.2将数据包装成 FormData类型文件
            let formData = new FormData();
            formData.append('title', title);
            formData.append('cover', cover);
            formData.append('categoryId', categoryId);
            formData.append('date', date);
            formData.append('content', content);
            formData.append('state', state);
            formData.append('id', get_params());

            myAjax('POST', '/admin/article/edit', formData, function (resData) {

                if (resData.code == 200) {
                    window.location = './article_list.html';
                }
            })
        }
    </script>
</body>

</html>